package com.itheima.二分法查找;

import java.util.Arrays;

public class MyBinarySearchDemo {
    public static void main(String[] args) {
        int[] arr = {11, 22, 33, 44, 55, 66};
        int num = 44;
        /*int index = binarySeachForIndex(arr, num);
        System.out.println(index);*/
        int index = Arrays.binarySearch(arr, num);
        System.out.println("index = " + index);
    }

    public static int binarySeachForIndex(int[] arr, int num) {
        int min = 0;
        int max = arr.length - 1;
        while (min <= max) {
            int mid = (min + max) / 2;
            if (arr[mid] > num) {
                max = mid - 1;
            } else if (arr[mid] < num) {
                min = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
}
